//
//  ZLListEmpty.m
//  ZLListEmpty
//
//  Created by 科大 on 2024/6/11.
//

#import "ZLListEmpty.h"
#import <objc/runtime.h>

static ZLListEmpty *zlListEmpty = nil;

@implementation ZLListEmpty

+ (instancetype)shared {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        zlListEmpty = [[ZLListEmpty alloc] init];
    });
    return zlListEmpty;
}

+ (void)configEmptyImage:(UIImage *)emptyImage emptyText:(NSString *)emptyText offset:(CGFloat)offset {
    [self shared].emptyImage = emptyImage;
    [self shared].emptyText = emptyText;
    [self shared].offset = offset;
}

+ (UIImage *)imageNamed:(NSString *)name {
    NSBundle *frameworkBundle = [NSBundle bundleForClass:[self class]];
    NSBundle *imageBundle = [NSBundle bundleWithPath:[frameworkBundle pathForResource:@"ZLListEmpty" ofType:@"bundle"]];
    UIImage *image = [UIImage imageNamed:name inBundle:imageBundle compatibleWithTraitCollection:nil];
    if (image == nil) {
        return [[UIImage alloc] init];
    }
    return image;
}

@end

@implementation UITableView (ZLEmpty)
static char zl_emptyEnable_key;

- (BOOL)zl_emptyEnable {
    NSNumber *number = objc_getAssociatedObject(self, &zl_emptyEnable_key);
    return [number boolValue];
}

- (void)setZl_emptyEnable:(BOOL)zl_emptyEnable {
    NSNumber *number = [NSNumber numberWithBool:zl_emptyEnable];
    objc_setAssociatedObject(self, &zl_emptyEnable_key, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        SEL originalSelector = @selector(reloadData);
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        SEL swizzledSelector = @selector(zl_reloadData);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        method_exchangeImplementations(originalMethod, swizzledMethod);
    });
}

- (void)zl_reloadData {
    [self zl_reloadData];
    if (!self.zl_emptyEnable) {
        return;
    }
    NSInteger count = 0;
    NSInteger sections = 1;
    if ([self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
        sections = [self.dataSource numberOfSectionsInTableView:self];
    }
    for (NSInteger s = 0; s < sections; s++) {
        NSInteger rows = [self.dataSource tableView:self numberOfRowsInSection:s];
        count += rows;
    }
    if (count == 0) {
        UIImageView *imageView = [self viewWithTag:999];
        if (!imageView) {
            imageView = [[UIImageView alloc] init];
        }
        imageView.tag = 999;
        imageView.image = [ZLListEmpty shared].emptyImage ?: [ZLListEmpty imageNamed:@"empty"];
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.height.mas_equalTo(140);
            make.centerY.equalTo(self).offset([ZLListEmpty shared].offset);
        }];
        
        UILabel *label = [self viewWithTag:998];
        if (!label) {
            label = [[UILabel alloc] init];
        }
        label.tag = 998;
        label.textColor = [UIColor colorWithWhite:0.4 alpha:1];
        label.text = [ZLListEmpty shared].emptyText.length ? [ZLListEmpty shared].emptyText : @"暂无数据";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor colorWithRed:174/255.0 green:178/255.0 blue:186/255.0 alpha:1];
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(imageView.mas_bottom).offset(10);
        }];
    } else {
        [[self viewWithTag:999] removeFromSuperview];
        [[self viewWithTag:998] removeFromSuperview];
    }
}

@end

@implementation UICollectionView (ZLEmpty)
static char zl_emptyEnable_key;

- (BOOL)zl_emptyEnable {
    NSNumber *number = objc_getAssociatedObject(self, &zl_emptyEnable_key);
    return [number boolValue];
}

- (void)setZl_emptyEnable:(BOOL)zl_emptyEnable {
    NSNumber *number = [NSNumber numberWithBool:zl_emptyEnable];
    objc_setAssociatedObject(self, &zl_emptyEnable_key, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        SEL originalSelector = @selector(reloadData);
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        SEL swizzledSelector = @selector(zl_reloadData);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        method_exchangeImplementations(originalMethod, swizzledMethod);
    });
}

- (void)zl_reloadData {
    [self zl_reloadData];
    if (!self.zl_emptyEnable) {
        return;
    }
    NSInteger count = 0;
    NSInteger sections = 0;
    if ([self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
        sections = [self.dataSource numberOfSectionsInCollectionView:self];
    }
    for (NSInteger s = 0; s < sections; s++) {
        NSInteger rows = [self.dataSource collectionView:self numberOfItemsInSection:s];
        count += rows;
    }
    if (count == 0) {
        UIImageView *imageView = [self viewWithTag:999];
        if (!imageView) {
            imageView = [[UIImageView alloc] init];
        }
        imageView.tag = 999;
        imageView.image = [ZLListEmpty shared].emptyImage ? [ZLListEmpty shared].emptyImage : [ZLListEmpty imageNamed:@"empty"];
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.height.mas_equalTo(140);
            make.centerY.equalTo(self).offset([ZLListEmpty shared].offset);
        }];
        
        UILabel *label = [self viewWithTag:998];
        if (!label) {
            label = [[UILabel alloc] init];
        }
        label.tag = 998;
        label.textColor = [UIColor colorWithWhite:0.4 alpha:1];
        label.text = [ZLListEmpty shared].emptyText.length ? [ZLListEmpty shared].emptyText : @"暂无数据";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor colorWithRed:174/255.0 green:178/255.0 blue:186/255.0 alpha:1];
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(imageView.mas_bottom).offset(10);
        }];
    } else {
        [[self viewWithTag:999] removeFromSuperview];
        [[self viewWithTag:998] removeFromSuperview];
    }
}

@end
